package top.laoshuzi.cloudplatform.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;

import java.io.IOException;

import top.laoshuzi.cloudplatform.MainActivity;
import top.laoshuzi.cloudplatform.tools.L;

public class AttitudeSeneorService extends Service implements SensorEventListener {

    private static final String I2CBUS_NAME = "I2C1";

    private Mpu6050 mpu6050;
    private Mpu6050Driver mpu6050Driver;
    private SensorManager mSensorManager;

    private SensorManager.DynamicSensorCallback dynamicSensorCallback = new SensorManager.DynamicSensorCallback() {
        @Override
        public void onDynamicSensorConnected(Sensor sensor) {
            mSensorManager.registerListener(
                    AttitudeSeneorService.this,
                    sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }

        @Override
        public void onDynamicSensorDisconnected(Sensor sensor) {
            mSensorManager.unregisterListener(AttitudeSeneorService.this);
        }
    };

    public AttitudeSeneorService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();

        startForeground(100, new Notification.Builder(this)
                .build());

        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mSensorManager.registerDynamicSensorCallback(dynamicSensorCallback);

        try {
            mpu6050 = new Mpu6050(I2CBUS_NAME);
            mpu6050Driver = new Mpu6050Driver(mpu6050);
            mpu6050Driver.register();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        mSensorManager.unregisterDynamicSensorCallback(dynamicSensorCallback);
        try {
            mpu6050Driver.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            L.w("Gyro:\t\t"
                    + event.values[0]
                    + "\t\t"
                    + event.values[1]
                    + "\t\t"
                    + event.values[2]);
        } else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            L.w("Acc:\t\t"
                    + event.values[0]
                    + "\t\t"
                    + event.values[1]
                    + "\t\t"
                    + event.values[2]);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
